<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>websocket_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_net_websocket_test.js.html">websocket_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.net.WebSocketTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.net.WebSocketTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.debug.EntryPointMonitor&#39;);
<a name="line19"></a>goog.require(&#39;goog.debug.ErrorHandler&#39;);
<a name="line20"></a>goog.require(&#39;goog.debug.entryPointRegistry&#39;);
<a name="line21"></a>goog.require(&#39;goog.events&#39;);
<a name="line22"></a>goog.require(&#39;goog.functions&#39;);
<a name="line23"></a>goog.require(&#39;goog.net.WebSocket&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line28"></a>
<a name="line29"></a>var webSocket;
<a name="line30"></a>var mockClock;
<a name="line31"></a>var pr;
<a name="line32"></a>var testUrl;
<a name="line33"></a>
<a name="line34"></a>var originalOnOpen = goog.net.WebSocket.prototype.onOpen_;
<a name="line35"></a>var originalOnClose = goog.net.WebSocket.prototype.onClose_;
<a name="line36"></a>var originalOnMessage = goog.net.WebSocket.prototype.onMessage_;
<a name="line37"></a>var originalOnError = goog.net.WebSocket.prototype.onError_;
<a name="line38"></a>
<a name="line39"></a>function setUp() {
<a name="line40"></a>  pr = new goog.testing.PropertyReplacer();
<a name="line41"></a>  pr.set(goog.global, &#39;WebSocket&#39;, MockWebSocket);
<a name="line42"></a>  mockClock = new goog.testing.MockClock(true);
<a name="line43"></a>  testUrl = &#39;ws://127.0.0.1:4200&#39;;
<a name="line44"></a>  testProtocol = &#39;xmpp&#39;;
<a name="line45"></a>}
<a name="line46"></a>
<a name="line47"></a>function tearDown() {
<a name="line48"></a>  pr.reset();
<a name="line49"></a>  goog.net.WebSocket.prototype.onOpen_ = originalOnOpen;
<a name="line50"></a>  goog.net.WebSocket.prototype.onClose_ = originalOnClose;
<a name="line51"></a>  goog.net.WebSocket.prototype.onMessage_ = originalOnMessage;
<a name="line52"></a>  goog.net.WebSocket.prototype.onError_ = originalOnError;
<a name="line53"></a>  goog.dispose(mockClock);
<a name="line54"></a>  goog.dispose(webSocket);
<a name="line55"></a>}
<a name="line56"></a>
<a name="line57"></a>function testOpenInUnsupportingBrowserThrowsException() {
<a name="line58"></a>  // Null out WebSocket to simulate lack of support.
<a name="line59"></a>  if (goog.global.WebSocket) {
<a name="line60"></a>    goog.global.WebSocket = null;
<a name="line61"></a>  }
<a name="line62"></a>
<a name="line63"></a>  webSocket = new goog.net.WebSocket();
<a name="line64"></a>  assertThrows(&#39;Open should fail if WebSocket is not defined.&#39;,
<a name="line65"></a>      function() {
<a name="line66"></a>        webSocket.open(testUrl);
<a name="line67"></a>      });
<a name="line68"></a>}
<a name="line69"></a>
<a name="line70"></a>function testOpenTwiceThrowsException() {
<a name="line71"></a>  webSocket = new goog.net.WebSocket();
<a name="line72"></a>  webSocket.open(testUrl);
<a name="line73"></a>  simulateOpenEvent(webSocket.webSocket_);
<a name="line74"></a>
<a name="line75"></a>  assertThrows(&#39;Attempting to open a second time should fail.&#39;,
<a name="line76"></a>      function() {
<a name="line77"></a>        webSocket.open(testUrl);
<a name="line78"></a>      });
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>function testSendWithoutOpeningThrowsException() {
<a name="line82"></a>  webSocket = new goog.net.WebSocket();
<a name="line83"></a>
<a name="line84"></a>  assertThrows(&#39;Send should fail if the web socket was not first opened.&#39;,
<a name="line85"></a>      function() {
<a name="line86"></a>        webSocket.send(&#39;test message&#39;);
<a name="line87"></a>      });
<a name="line88"></a>}
<a name="line89"></a>
<a name="line90"></a>function testOpenWithProtocol() {
<a name="line91"></a>  webSocket = new goog.net.WebSocket();
<a name="line92"></a>  webSocket.open(testUrl, testProtocol);
<a name="line93"></a>  var ws = webSocket.webSocket_;
<a name="line94"></a>  simulateOpenEvent(ws);
<a name="line95"></a>  assertEquals(testUrl, ws.url);
<a name="line96"></a>  assertEquals(testProtocol, ws.protocol);
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>function testOpenAndClose() {
<a name="line100"></a>  webSocket = new goog.net.WebSocket();
<a name="line101"></a>  assertFalse(webSocket.isOpen());
<a name="line102"></a>  webSocket.open(testUrl);
<a name="line103"></a>  var ws = webSocket.webSocket_;
<a name="line104"></a>  simulateOpenEvent(ws);
<a name="line105"></a>  assertTrue(webSocket.isOpen());
<a name="line106"></a>  assertEquals(testUrl, ws.url);
<a name="line107"></a>  webSocket.close();
<a name="line108"></a>  simulateCloseEvent(ws);
<a name="line109"></a>  assertFalse(webSocket.isOpen());
<a name="line110"></a>}
<a name="line111"></a>
<a name="line112"></a>function testReconnectionDisabled() {
<a name="line113"></a>  // Construct the web socket and disable reconnection.
<a name="line114"></a>  webSocket = new goog.net.WebSocket(false);
<a name="line115"></a>
<a name="line116"></a>  // Record how many times open is called.
<a name="line117"></a>  pr.set(webSocket, &#39;open&#39;, goog.testing.recordFunction(webSocket.open));
<a name="line118"></a>
<a name="line119"></a>  // Open the web socket.
<a name="line120"></a>  webSocket.open(testUrl);
<a name="line121"></a>  assertEquals(0, webSocket.reconnectAttempt_);
<a name="line122"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line123"></a>  assertFalse(webSocket.isOpen());
<a name="line124"></a>
<a name="line125"></a>  // Simulate failure.
<a name="line126"></a>  var ws = webSocket.webSocket_;
<a name="line127"></a>  simulateCloseEvent(ws);
<a name="line128"></a>  assertFalse(webSocket.isOpen());
<a name="line129"></a>  assertEquals(0, webSocket.reconnectAttempt_);
<a name="line130"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line131"></a>
<a name="line132"></a>  // Make sure a reconnection doesn&#39;t happen.
<a name="line133"></a>  mockClock.tick(100000);
<a name="line134"></a>  assertEquals(0, webSocket.reconnectAttempt_);
<a name="line135"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line136"></a>}
<a name="line137"></a>
<a name="line138"></a>function testReconnectionWithFailureOnFirstOpen() {
<a name="line139"></a>  // Construct the web socket with a linear back-off.
<a name="line140"></a>  webSocket = new goog.net.WebSocket(true, linearBackOff);
<a name="line141"></a>
<a name="line142"></a>  // Record how many times open is called.
<a name="line143"></a>  pr.set(webSocket, &#39;open&#39;, goog.testing.recordFunction(webSocket.open));
<a name="line144"></a>
<a name="line145"></a>  // Open the web socket.
<a name="line146"></a>  webSocket.open(testUrl, testProtocol);
<a name="line147"></a>  assertEquals(0, webSocket.reconnectAttempt_);
<a name="line148"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line149"></a>  assertFalse(webSocket.isOpen());
<a name="line150"></a>
<a name="line151"></a>  // Simulate failure.
<a name="line152"></a>  var ws = webSocket.webSocket_;
<a name="line153"></a>  simulateCloseEvent(ws);
<a name="line154"></a>  assertFalse(webSocket.isOpen());
<a name="line155"></a>  assertEquals(1, webSocket.reconnectAttempt_);
<a name="line156"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line157"></a>
<a name="line158"></a>  // Make sure the reconnect doesn&#39;t happen before it should.
<a name="line159"></a>  mockClock.tick(linearBackOff(0) - 1);
<a name="line160"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line161"></a>  mockClock.tick(1);
<a name="line162"></a>  assertEquals(2, webSocket.open.getCallCount());
<a name="line163"></a>
<a name="line164"></a>  // Simulate another failure.
<a name="line165"></a>  simulateCloseEvent(ws);
<a name="line166"></a>  assertFalse(webSocket.isOpen());
<a name="line167"></a>  assertEquals(2, webSocket.reconnectAttempt_);
<a name="line168"></a>  assertEquals(2, webSocket.open.getCallCount());
<a name="line169"></a>
<a name="line170"></a>  // Make sure the reconnect doesn&#39;t happen before it should.
<a name="line171"></a>  mockClock.tick(linearBackOff(1) - 1);
<a name="line172"></a>  assertEquals(2, webSocket.open.getCallCount());
<a name="line173"></a>  mockClock.tick(1);
<a name="line174"></a>  assertEquals(3, webSocket.open.getCallCount());
<a name="line175"></a>
<a name="line176"></a>  // Simulate connection success.
<a name="line177"></a>  simulateOpenEvent(ws);
<a name="line178"></a>  assertEquals(0, webSocket.reconnectAttempt_);
<a name="line179"></a>  assertEquals(3, webSocket.open.getCallCount());
<a name="line180"></a>
<a name="line181"></a>  // Make sure the reconnection has the same url and protocol.
<a name="line182"></a>  assertEquals(testUrl, ws.url);
<a name="line183"></a>  assertEquals(testProtocol, ws.protocol);
<a name="line184"></a>
<a name="line185"></a>  // Ensure no further calls to open are made.
<a name="line186"></a>  mockClock.tick(linearBackOff(10));
<a name="line187"></a>  assertEquals(3, webSocket.open.getCallCount());
<a name="line188"></a>}
<a name="line189"></a>
<a name="line190"></a>function testReconnectionWithFailureAfterOpen() {
<a name="line191"></a>  // Construct the web socket with a linear back-off.
<a name="line192"></a>  webSocket = new goog.net.WebSocket(true, fibonacciBackOff);
<a name="line193"></a>
<a name="line194"></a>  // Record how many times open is called.
<a name="line195"></a>  pr.set(webSocket, &#39;open&#39;, goog.testing.recordFunction(webSocket.open));
<a name="line196"></a>
<a name="line197"></a>  // Open the web socket.
<a name="line198"></a>  webSocket.open(testUrl);
<a name="line199"></a>  assertEquals(0, webSocket.reconnectAttempt_);
<a name="line200"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line201"></a>  assertFalse(webSocket.isOpen());
<a name="line202"></a>
<a name="line203"></a>  // Simulate connection success.
<a name="line204"></a>  var ws = webSocket.webSocket_;
<a name="line205"></a>  simulateOpenEvent(ws);
<a name="line206"></a>  assertEquals(0, webSocket.reconnectAttempt_);
<a name="line207"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line208"></a>
<a name="line209"></a>  // Let some time pass, then fail the connection.
<a name="line210"></a>  mockClock.tick(100000);
<a name="line211"></a>  simulateCloseEvent(ws);
<a name="line212"></a>  assertFalse(webSocket.isOpen());
<a name="line213"></a>  assertEquals(1, webSocket.reconnectAttempt_);
<a name="line214"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line215"></a>
<a name="line216"></a>  // Make sure the reconnect doesn&#39;t happen before it should.
<a name="line217"></a>  mockClock.tick(fibonacciBackOff(0) - 1);
<a name="line218"></a>  assertEquals(1, webSocket.open.getCallCount());
<a name="line219"></a>  mockClock.tick(1);
<a name="line220"></a>  assertEquals(2, webSocket.open.getCallCount());
<a name="line221"></a>
<a name="line222"></a>  // Simulate connection success.
<a name="line223"></a>  ws = webSocket.webSocket_;
<a name="line224"></a>  simulateOpenEvent(ws);
<a name="line225"></a>  assertEquals(0, webSocket.reconnectAttempt_);
<a name="line226"></a>  assertEquals(2, webSocket.open.getCallCount());
<a name="line227"></a>
<a name="line228"></a>  // Ensure no further calls to open are made.
<a name="line229"></a>  mockClock.tick(fibonacciBackOff(10));
<a name="line230"></a>  assertEquals(2, webSocket.open.getCallCount());
<a name="line231"></a>}
<a name="line232"></a>
<a name="line233"></a>function testExponentialBackOff() {
<a name="line234"></a>  assertEquals(1000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(0));
<a name="line235"></a>  assertEquals(2000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(1));
<a name="line236"></a>  assertEquals(4000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(2));
<a name="line237"></a>  assertEquals(60000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(6));
<a name="line238"></a>  assertEquals(60000, goog.net.WebSocket.EXPONENTIAL_BACKOFF_(7));
<a name="line239"></a>}
<a name="line240"></a>
<a name="line241"></a>function testEntryPointRegistry() {
<a name="line242"></a>  var monitor = new goog.debug.EntryPointMonitor();
<a name="line243"></a>  var replacement = function() {};
<a name="line244"></a>  monitor.wrap = goog.testing.recordFunction(
<a name="line245"></a>      goog.functions.constant(replacement));
<a name="line246"></a>
<a name="line247"></a>  goog.debug.entryPointRegistry.monitorAll(monitor);
<a name="line248"></a>  assertTrue(monitor.wrap.getCallCount() &gt;= 1);
<a name="line249"></a>  assertEquals(replacement, goog.net.WebSocket.prototype.onOpen_);
<a name="line250"></a>  assertEquals(replacement, goog.net.WebSocket.prototype.onClose_);
<a name="line251"></a>  assertEquals(replacement, goog.net.WebSocket.prototype.onMessage_);
<a name="line252"></a>  assertEquals(replacement, goog.net.WebSocket.prototype.onError_);
<a name="line253"></a>}
<a name="line254"></a>
<a name="line255"></a>function testErrorHandlerCalled() {
<a name="line256"></a>  var errorHandlerCalled = false;
<a name="line257"></a>  var errorHandler = new goog.debug.ErrorHandler(function() {
<a name="line258"></a>    errorHandlerCalled = true;
<a name="line259"></a>  });
<a name="line260"></a>  goog.net.WebSocket.protectEntryPoints(errorHandler);
<a name="line261"></a>
<a name="line262"></a>  webSocket = new goog.net.WebSocket();
<a name="line263"></a>  goog.events.listenOnce(webSocket, goog.net.WebSocket.EventType.OPENED,
<a name="line264"></a>      function() {
<a name="line265"></a>        throw Error();
<a name="line266"></a>      });
<a name="line267"></a>
<a name="line268"></a>  webSocket.open(testUrl);
<a name="line269"></a>  var ws = webSocket.webSocket_;
<a name="line270"></a>  assertThrows(function() {
<a name="line271"></a>    simulateOpenEvent(ws);
<a name="line272"></a>  });
<a name="line273"></a>
<a name="line274"></a>  assertTrue(&#39;Error handler callback should be called when registered as &#39; +
<a name="line275"></a>      &#39;protecting the entry points.&#39;, errorHandlerCalled);
<a name="line276"></a>}
<a name="line277"></a>
<a name="line278"></a>
<a name="line279"></a>/**
<a name="line280"></a> * Simulates the browser firing the open event for the given web socket.
<a name="line281"></a> * @param {MockWebSocket} ws The mock web socket.
<a name="line282"></a> */
<a name="line283"></a>function simulateOpenEvent(ws) {
<a name="line284"></a>  ws.readyState = goog.net.WebSocket.ReadyState_.OPEN;
<a name="line285"></a>  ws.onopen();
<a name="line286"></a>}
<a name="line287"></a>
<a name="line288"></a>
<a name="line289"></a>/**
<a name="line290"></a> * Simulates the browser firing the close event for the given web socket.
<a name="line291"></a> * @param {MockWebSocket} ws The mock web socket.
<a name="line292"></a> */
<a name="line293"></a>function simulateCloseEvent(ws) {
<a name="line294"></a>  ws.readyState = goog.net.WebSocket.ReadyState_.CLOSED;
<a name="line295"></a>  ws.onclose({data: &#39;mock close event&#39;});
<a name="line296"></a>}
<a name="line297"></a>
<a name="line298"></a>
<a name="line299"></a>/**
<a name="line300"></a> * Strategy for reconnection that backs off linearly with a 1 second offset.
<a name="line301"></a> * @param {number} attempt The number of reconnects since the last connection.
<a name="line302"></a> * @return {number} The amount of time to the next reconnect, in milliseconds.
<a name="line303"></a> */
<a name="line304"></a>function linearBackOff(attempt) {
<a name="line305"></a>  return (attempt * 1000) + 1000;
<a name="line306"></a>}
<a name="line307"></a>
<a name="line308"></a>
<a name="line309"></a>/**
<a name="line310"></a> * Strategy for reconnection that backs off with the fibonacci pattern.  It is
<a name="line311"></a> * offset by 5 seconds so the first attempt will happen after 5 seconds.
<a name="line312"></a> * @param {number} attempt The number of reconnects since the last connection.
<a name="line313"></a> * @return {number} The amount of time to the next reconnect, in milliseconds.
<a name="line314"></a> */
<a name="line315"></a>function fibonacciBackOff(attempt) {
<a name="line316"></a>  return (fibonacci(attempt) * 1000) + 5000;
<a name="line317"></a>}
<a name="line318"></a>
<a name="line319"></a>
<a name="line320"></a>/**
<a name="line321"></a> * Computes the desired fibonacci number.
<a name="line322"></a> * @param {number} n The nth desired fibonacci number.
<a name="line323"></a> * @return {number} The nth fibonacci number.
<a name="line324"></a> */
<a name="line325"></a>function fibonacci(n) {
<a name="line326"></a>  if (n == 0) {
<a name="line327"></a>    return 0;
<a name="line328"></a>  } else if (n == 1) {
<a name="line329"></a>    return 1;
<a name="line330"></a>  } else {
<a name="line331"></a>    return fibonacci(n - 2) + fibonacci(n - 1);
<a name="line332"></a>  }
<a name="line333"></a>}
<a name="line334"></a>
<a name="line335"></a>
<a name="line336"></a>
<a name="line337"></a>/**
<a name="line338"></a> * Mock WebSocket constructor.
<a name="line339"></a> * @param {string} url The url to the web socket server.
<a name="line340"></a> * @param {string} protocol The protocol to use.
<a name="line341"></a> * @constructor
<a name="line342"></a> */
<a name="line343"></a>MockWebSocket = function(url, protocol) {
<a name="line344"></a>  this.url = url;
<a name="line345"></a>  this.protocol = protocol;
<a name="line346"></a>  this.readyState = goog.net.WebSocket.ReadyState_.CONNECTING;
<a name="line347"></a>};
<a name="line348"></a>
<a name="line349"></a>
<a name="line350"></a>/**
<a name="line351"></a> * Mocks out the close method of the WebSocket.
<a name="line352"></a> */
<a name="line353"></a>MockWebSocket.prototype.close = function() {
<a name="line354"></a>  this.readyState = goog.net.WebSocket.ReadyState_.CLOSING;
<a name="line355"></a>};
<a name="line356"></a>
<a name="line357"></a>
<a name="line358"></a>/**
<a name="line359"></a> * Mocks out the send method of the WebSocket.
<a name="line360"></a> */
<a name="line361"></a>MockWebSocket.prototype.send = function() {
<a name="line362"></a>  // Nothing to do here.
<a name="line363"></a>};
</pre>


</body>
</html>
